#!/usr/bin/perl
# 
# Exercise 8.10
# 
# Some codons are more likely than others to occur in random DNA. For instance, 
# there are 6 of the 64 possible codons that code for the amino acid serine, but only 2 
# of the 64 codes for phenylalanine. Write a subroutine that, given an amino acid, 
# returns the probability that it's coded by a randomly generated codon (see 
# Chapter 7).
#
# Answer to Exercise 8.10

# If you look at the answer to Exercise 8.8, the hash %reverse_genetic_code shows
# all the codons that code for an amino acid.  So our subroutine can just see how
# many codons code for the amino acid, and divide that number by 64.
#

use strict;
use warnings;

print "Input one-character amino acid: ";
my $aa = <STDIN>;
chomp $aa;

print "The probability that a random codon codes for $aa is ", aa_probability($aa),"\n";

exit;

##################################################
# Subroutines
##################################################

sub aa_probability {

	my($aa) = @_;

	my(%reverse_genetic_code) = (
	
	'A' => 'GCA GCC GCG GCT',		# Alanine
	'C' => 'TGC TGT',			# Cysteine
	'D' => 'GAC GAT',			# Aspartic Acid
	'E' => 'GAA GAG',			# Glutamic Acid
	'F' => 'TTC TTT',			# Phenylalanine
	'G' => 'GGA GGC GGG GGT',		# Glycine
	'H' => 'CAC CAT',			# Histidine
	'I' => 'ATA ATC ATT',			# Isoleucine
	'K' => 'AAA AAG',			# Lysine
	'L' => 'CTA CTC CTG CTT TTA TTG',	# Leucine
	'M' => 'ATG',				# Methionine
	'N' => 'AAC AAT',			# Asparagine
	'P' => 'CCA CCC CCG CCT',		# Proline
	'Q' => 'CAA CAG',			# Glutamine
	'R' => 'CGA CGC CGG CGT AGA AGG',	# Arginine
	'S' =>'TCA TCC TCG TCT AGC AGT',	# Serine
	'T' => 'ACA ACC ACG ACT',		# Threonine
	'V' => 'GTA GTC GTG GTT',		# Valine
	'W' => 'TGG',				# Tryptophan
	'Y' => 'TAC TAT',			# Tyrosine
	'_' => 'TAA TAG TGA',			# Stop
	);

	# If there's a non-amino acid (case-insensitive) character, return "false"
	if($aa =~ /[^ACDEFGHIKLMNPQRSTVWY_]/i) { return 0; }

	# Otherwise

	my $number_of_codons = split(' ', $reverse_genetic_code{$aa});

	return $number_of_codons / 64;
}
